var currPage = 1;
var dataDetail=null;

pageIndex = sessionStorage.getItem("pageIndex")
$(".purchasing_slide_nav li a").eq(pageIndex).addClass("on layui-anim layui-anim-scale").siblings(".purchasing_slide_nav li a").removeClass("on layui-anim layui-anim-scale");
$(".purchasing_slide_content_wrap > div").eq(pageIndex).show().siblings(".purchasing_slide_content_wrap > div").hide();
;layui.define(function(e) {
    layui.use(['table','layer','form','laydate'], function(){
         var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;
         
         $(".purchasing_slide_nav li a").each(function (e) {
             $(this).click(function () {
            	 sessionStorage.setItem('pageIndex',e);
            	 flag = false;
                 $(".purchasing_slide_nav li a").removeClass("on layui-anim layui-anim-scale").eq(e).addClass("on layui-anim layui-anim-scale");
                 $(".purchasing_slide_content_wrap > div").hide().eq(e).show();
                 reloadTablePurchase();
                 reloadTableDeliver();
                 reloadTableIntentio();
                 reloadTableDeliver();
                 reloadTableSign();
                 reloadTableLose();
             })
         })
         
         
     //重新渲染表单
     function renderForm(){
       layui.use('form', function(){
        	var form = layui.form;
        	form.render();
       });
     }
         
     //日期控件
     layui.use(['laydate'], function(){
         var laydate = layui.laydate;
         //常规用法(列表)
         laydate.render({
           elem: '#createTime'
         });
         laydate.render({
        	 elem: '#endDate'
         });
         //采购、延期订订单
         laydate.render({
           elem: '#createTimePurcahse'
         });
         laydate.render({
        	 elem: '#endDatePurcahse'
         });
         // 发货单
         laydate.render({
        	 elem: '#createTimeDeliver'
         });
         laydate.render({
        	 elem: '#endDateDeliver'
         });
         // 签收单
         laydate.render({
        	 elem: '#createSign'
         });
         laydate.render({
        	 elem: '#endDateSign'
         });
         // 失效订单
         laydate.render({
        	 elem: '#createLose'
         });
         laydate.render({
        	 elem: '#endDateLose'
         });
     });  
     
     $(function(){
     	loadDealer();
     })
     function loadDealer(){
     	$.ajax({
     		url: hostUrl+'/stockPlatform/getDealer',
             type: "post",
             data: {},
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             success: function(data) {
             	var mydata=data.data.dealer_list;
             	for ( var i = 0; i <mydata.length; i++){
     				var value=mydata[i].id;
     				var name=mydata[i].dealer_name;
     				$("#dealerId option:eq(0)").after("<option value='"+value+"'>"+name+"</option>");
     				$("#dealerIdPurcahse option:eq(0)").after("<option value='"+value+"'>"+name+"</option>");
     				$("#dealerIdDeliver option:eq(0)").after("<option value='"+value+"'>"+name+"</option>");
     				$("#dealerIdSign option:eq(0)").after("<option value='"+value+"'>"+name+"</option>");
     				$("#dealerIdLose option:eq(0)").after("<option value='"+value+"'>"+name+"</option>");
     			}
             	renderForm();
             }
         });
     }
     
     /****************************意向订单列表*********************************/
     
     // 意向单表格数据start
     var purchaseIntention=layui.table;
     purchaseIntention.render({
          elem: '#idIntentioTable'
         ,url: hostUrl + '/purchaseIntentioOrderHead/queryPurchaseList'
          ,cols: [[ //标题栏
              {field: 'order_no', title: '订单号',width: 190,fixed:'left'}
              ,{field: 'dealer_name', title: '经销商',fixed:'left'}
              ,{field: 'pickup_address', title: '发货地址'}
              ,{field: 'create_time', title: '下订时间',width: 180,
                  templet :function (row){
                  	 var ret="";
	                   	 if(row.create_time=="" || row.create_time==null){
	                   		 
	                   	 }else{
	                   		 ret=new Date(row.create_time).Format("yyyy-MM-dd hh:mm:ss");
	                   	 }
	                   	 return ret;
                  }   
          	 }
              ,{field: 'order_qty', title: '下订数量'}
              ,{field: 'payment_type', title: '支付方式'}
              ,{field: 'receive_name', title: '接车人姓名'}
              ,{field: 'receive_tel', title: '接车人电话'}
              ,{field: 'delivery_way_name', title: '配送方式'}
              ,{field: 'platform_status_name', title: '订单状态'}
              ,{field: 'tool',title: '操作',fixed: 'right', align:'center', toolbar: '#barPurchase'}
          ]]
          ,id: 'listReloadIntentio'
          ,page: true //是否显示分页
      });
   //查询按钮绑定点击事件
     $("#searchBtnIntentio").on("click", function () {
    	 reloadTableIntentio();
     })
     //表刷新方法    查询方法调用
     var reloadTableIntentio = function (page) {
         var length = arguments.length;   //  实际传入参数的长度
         if (length == 0) {
             page = 1;
         }
         table.reload("listReloadIntentio", { //此处是上文提到的 初始化标识id
             where: {
                 "orderOrDealer": $("#orderOrDealer").val(),
                 "createTime": $("#createTime").val(),
                 "endDate": $("#endDate").val(),
                 "dealerId": $("#dealerId").val(),
                 "platformStatus": $("#platformStatus").val()
             }, page: {
                 curr: page //重新从第 page 页开始 默认从第一页开始重载
             }
         });
     };
     //监听意向单工具条
     purchaseIntention.on('tool(listTablePurchase)', function(obj){
         var data = obj.data;
         dataDetail = data;
         var url = null;
         if(dataDetail.platform_status == 1){
        	 url = hostUrl +'/purchase/intentioOrderDetail.do'
         }else if(dataDetail.platform_status == 2){
        	 url = hostUrl +'/purchase/financialPaymentDetail.do'
         }else if(dataDetail.platform_status == 3){
        	 url = hostUrl +'/purchase/inputCarNumberDetail.do'
         }else if(dataDetail.platform_status == 4){
        	 url = hostUrl +'/purchase/generatePurchaseOrder.do'
         }else if(dataDetail.platform_status == 5){
        	 url = hostUrl +'/purchase/inStockDetail.do';
         }else if(dataDetail.platform_status == 6){
        	 url = hostUrl +'/purchase/inStockDetail.do';
         } else if(dataDetail.platform_status == 7){
        	 url = hostUrl +'/purchase/signOrderDetailInfo.do'
         }
    	 if(obj.event === 'enter'){
    		 var index = layer.open({
    			 type: 2
    			 ,id: (new Date()).valueOf()
    			 ,title: '进入'
				 ,content: url
				 ,area: ['500px', '500px']
    		 ,maxmin: true
    		 ,success: function(layero, index){
    			 var body = layer.getChildFrame('body', index);
    			 var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
    		 }
    		 ,yes: function (index,layero) {
    			 
    			 //return false 开启该代码可禁止点击该按钮关闭
    		 }
    		 ,btn1: function (index,layero) {
    			 //return false 开启该代码可禁止点击该按钮关闭
    		 }
    		 });
    		 layer.full(index);
    	 }
     });
     /****************************采购/延期订单列表*********************************/
     var purchaseOrder=layui.table;
     purchaseOrder.render({
          elem: '#idPurchaseTable'
         ,url: hostUrl + '/purchaseOrderBaseHead/queryPurchaseList'
          ,cols: [[ //标题栏
              {field: 'purchase_order_no', title: '订单号',width: 190,fixed:'left'}
              ,{field: 'dealer_name', title: '经销商',fixed:'left'}
              ,{field: 'pickup_address', title: '发货地址'}
              ,{field: 'create_time', title: '建档时间',width: 180,
                  templet :function (row){
                  	 var ret="";
	                   	 if(row.create_time=="" || row.create_time==null){
	                   		 
	                   	 }else{
	                   		 ret=new Date(row.create_time).Format("yyyy-MM-dd hh:mm:ss");
	                   	 }
	                   	 return ret;
                  }   
          	 }
              ,{field: 'order_qty', title: '车辆数量'}
              ,{field: 'payment_type_name', title: '支付方式'}
              ,{field: 'receive_name', title: '接车人姓名'}
              ,{field: 'receive_tel', title: '接车人电话'}
              ,{field: 'delivery_way_name', title: '配送方式'}
              ,{field: 'purchase_status_name', title: '当前状态'}
              ,{field: 'tool',title: '操作',fixed: 'right', align:'center', toolbar: '#barPurchaseOrder'}
          ]]
          ,id: 'listReloadPurchase'
          ,page: true //是否显示分页
      });
     //查询按钮绑定点击事件
     $("#searchBtnPurchase").on("click", function () {
    	 reloadTablePurchase();
     })
      //表刷新方法    查询方法调用
        var reloadTablePurchase = function (page) {
            var length = arguments.length;   //  实际传入参数的长度
            if (length == 0) {
                page = 1;
            }
            table.reload("listReloadPurchase", { //此处是上文提到的 初始化标识id
                where: {
                    "orderOrDealer": $("#orderOrDealerPurcahse").val(),
                    "createTime": $("#createTimePurcahse").val(),
                    "endDate": $("#endDatePurcahse").val(),
                    "dealerId": $("#dealerIdPurcahse").val(),
                    "orderType": $("#orderType").val()
                }, page: {
                    curr: page //重新从第 page 页开始 默认从第一页开始重载
                }
            });
        };
        
        //监听意向单工具条
        purchaseOrder.on('tool(listPurchaseOrderTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'enter'){
            	 dataDetail = data;
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '进入'
                    ,content: hostUrl +'/purchase/deliverOrderDetail.do'
                    ,maxmin: true
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    }
                    ,yes: function (index,layero) {

                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                    ,btn1: function (index,layero) {
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });
                layer.full(index);
            }else if(obj.event === 'edit'){
            	 dataDetail = data;
                 var index = layer.open({
                     type: 2
                     ,id: (new Date()).valueOf()
                     ,title: '进入'
                     ,content: hostUrl +'/purchase/postponeGenPurchaeDetail.do'
                     ,area: ['500px', '500px']
                     ,maxmin: true
                     ,success: function(layero, index){
                         var body = layer.getChildFrame('body', index);
                         var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                     }
                     ,yes: function (index,layero) {

                         //return false 开启该代码可禁止点击该按钮关闭
                     }
                     ,btn1: function (index,layero) {
                         //return false 开启该代码可禁止点击该按钮关闭
                     }
                 });
                 layer.full(index);
            }
        });
     /**************************** 发货单列表*********************************/
        var purchaseDeliver=layui.table;
        purchaseDeliver.render({
             elem: '#idPurchaseDeliverTable'
            ,url: hostUrl + '/purchaseOrderBaseHead/queryDeliveryOrderList'
             ,cols: [[ //标题栏
                 {field: 'purchase_order_no', title: '订单号',width: 190,fixed:'left'}
                 ,{field: 'dealer_name', title: '经销商',fixed:'left'}
                 ,{field: 'pickup_address', title: '发货地址'}
                 ,{field: 'delivery_time', title: '建档时间',width: 180,
                     templet :function (row){
                     	 var ret="";
   	                   	 if(row.delivery_time=="" || row.delivery_time==null){
   	                   		 
   	                   	 }else{
   	                   		 ret=new Date(row.delivery_time).Format("yyyy-MM-dd hh:mm:ss");
   	                   	 }
   	                   	 return ret;
                     }   
             	 }
                 ,{field: 'order_qty', title: '车辆数量'}
                 ,{field: 'payment_type_name', title: '支付方式'}
                 ,{field: 'receive_name', title: '接车人姓名'}
                 ,{field: 'receive_tel', title: '接车人电话'}
                 ,{field: 'delivery_way_name', title: '配送方式'}
                 ,{field: 'financial_status_name', title: '当前状态'}
                 ,{field: 'tool',title: '操作',fixed: 'right', align:'center',width: 180,toolbar: '#barPurchaseDeliver'}
             ]]
             ,id: 'listReloadDeliver'
             ,page: true //是否显示分页
         });
        //查询按钮绑定点击事件
        $("#searchBtnDeliver").on("click", function () {
        	reloadTableDeliver();
        })
         //表刷新方法    查询方法调用
        var reloadTableDeliver = function (page) {
            var length = arguments.length;   //  实际传入参数的长度
            if (length == 0) {
                page = 1;
            }
            table.reload("listReloadDeliver", { //此处是上文提到的 初始化标识id
                where: {
                    "orderNo": $("#orderDeliver").val(),
                    "deliveryTime": $("#createTimeDeliver").val(),
                    "endDate": $("#endDateDeliver").val(),
                    "dealerId": $("#dealerIdDeliver").val()
                }, page: {
                    curr: page //重新从第 page 页开始 默认从第一页开始重载
                }
            });
        };
        
        //监听意向单工具条
        purchaseDeliver.on('tool(listDeliverOrderTable)', function(obj){
        	 var data = obj.data;
             if(obj.event === 'enter'){
             	 dataDetail = data;
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '进入'
                    ,content: hostUrl +'/purchase/inputReimbursementDeliveryDetail.do' // 录入还款信息
                    ,area: ['500px', '500px']
                    ,maxmin: true
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    }
                    ,yes: function (index,layero) {

                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                    ,btn1: function (index,layero) {
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });
                layer.full(index);
            }else if(obj.event === 'detail'){
            	 dataDetail = data;
                 var index = layer.open({
                     type: 2
                     ,id: (new Date()).valueOf()
                     ,title: '进入'
                     ,content: hostUrl +'/purchase/inputReimbursementInfo.do' //详情信息
                     ,area: ['500px', '500px']
                     ,maxmin: true
                     ,success: function(layero, index){
                         var body = layer.getChildFrame('body', index);
                         var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                     }
                     ,yes: function (index,layero) {

                         //return false 开启该代码可禁止点击该按钮关闭
                     }
                     ,btn1: function (index,layero) {
                         //return false 开启该代码可禁止点击该按钮关闭
                     }
                 });
                 layer.full(index);
            }
        });
     /****************************签收单列表*********************************/
        
        var purchaseSign = layui.table;
        purchaseSign.render({
             elem: '#idPurchaseSignTable'
            ,url: hostUrl + '/purchaseOrderBaseHead/querySignOrderList'
             ,cols: [[ //标题栏
                 {field: 'purchase_order_no', title: '订单号',width: 190,fixed:'left'}
                 ,{field: 'dealer_name', title: '经销商',fixed:'left'}
                 ,{field: 'pickup_address', title: '发货地址'}
                 ,{field: 'sign_date', title: '签收时间',width: 180,
                     templet :function (row){
                     	 var ret="";
   	                   	 if(row.delivery_time=="" || row.delivery_time==null){
   	                   		 
   	                   	 }else{
   	                   		 ret=new Date(row.delivery_time).Format("yyyy-MM-dd hh:mm:ss");
   	                   	 }
   	                   	 return ret;
                     }   
             	 }
                 ,{field: 'order_qty', title: '车辆数量'}
                 ,{field: 'payment_type_name', title: '支付方式'}
                 ,{field: 'receive_name', title: '接车人姓名'}
                 ,{field: 'receive_tel', title: '接车人电话'}
                 ,{field: 'delivery_way_name', title: '配送方式'}
                 ,{field: 'purchase_status_name', title: '当前状态'}
                 ,{field: 'tool',title: '操作',fixed: 'right', align:'center',width: 180, toolbar: '#barPurchaseSign'}
             ]]
             ,id: 'listReloadSigns'
             ,page: true //是否显示分页
         });
        
        //查询按钮绑定点击事件
        $("#searchBtnSign").on("click", function () {
        	reloadTableSign();
        })
         //表刷新方法    查询方法调用
        var reloadTableSign = function (page) {
            var length = arguments.length;   //  实际传入参数的长度
            if (length == 0) {
                page = 1;
            }
            table.reload("listReloadSigns", { //此处是上文提到的 初始化标识id
                where: {
                    "orderNo": $("#orderSign").val(),
                    "signDate": $("#createSign").val(),
                    "endDate": $("#endDateSign").val(),
                    "dealerId": $("#dealerIdSign").val()
                }, page: {
                    curr: page //重新从第 page 页开始 默认从第一页开始重载
                }
            });
        };
        
        
        
      //监听意向单工具条
       purchaseSign.on('tool(listSignOrderTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'enter'){
            	 dataDetail = data;
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '进入'
                    ,content: hostUrl +'/purchase/inputReimbursementDetail.do' // 录入还款信息
                    ,area: ['500px', '500px']
                    ,maxmin: true
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    }
                    ,yes: function (index,layero) {

                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                    ,btn1: function (index,layero) {
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });
                layer.full(index);
            }else if(obj.event === 'detail'){
            	 dataDetail = data;
                 var index = layer.open({
                     type: 2
                     ,id: (new Date()).valueOf()
                     ,title: '进入'
                     ,content:  hostUrl +'/purchase/inputSignInfo.do' // 签收单信息
                     ,area: ['500px', '500px']
                     ,maxmin: true
                     ,success: function(layero, index){
                         var body = layer.getChildFrame('body', index);
                         var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                     }
                     ,yes: function (index,layero) {

                         //return false 开启该代码可禁止点击该按钮关闭
                     }
                     ,btn1: function (index,layero) {
                         //return false 开启该代码可禁止点击该按钮关闭
                     }
                 });
                 layer.full(index);
            }
        });
        
     /****************************失效单列表*********************************/
      
      var purchaseLose=layui.table;
      purchaseLose.render({
           elem: '#idPurchaseLoseTable'
          ,url: hostUrl + '/purchaseIntentioOrderHead/queryInvalidationList'
           ,cols: [[ //标题栏
               {field: 'order_no', title: '订单号',width: 190,fixed:'left'}
               ,{field: 'dealer_name', title: '经销商',fixed:'left'}
               ,{field: 'pickup_address', title: '发货地址'}
               ,{field: 'create_time', title: '下订时间',width: 180,
                   templet :function (row){
                   	 var ret="";
 	                   	 if(row.create_time=="" || row.create_time==null){
 	                   		 
 	                   	 }else{
 	                   		 ret=new Date(row.create_time).Format("yyyy-MM-dd hh:mm:ss");
 	                   	 }
 	                   	 return ret;
                   }   
           	 }
               ,{field: 'order_qty', title: '车辆数量'}
               ,{field: 'payment_type', title: '支付方式'}
               ,{field: 'receive_name', title: '接车人姓名'}
               ,{field: 'receive_tel', title: '接车人电话'}
               ,{field: 'delivery_way_name', title: '配送方式'}
               ,{field: 'invalid_type_name', title: '失效类型'}
               ,{field: 'tool',title: '操作',fixed: 'right', align:'center', toolbar: '#barLoseOrder'}
           ]]
           ,id: 'listReloadLose'
           ,page: true //是否显示分页
       });
        //查询按钮绑定点击事件
        $("#searchBtnLose").on("click", function () {
        	reloadTableLose();
        })
         //表刷新方法    查询方法调用
        var reloadTableLose = function (page) {
            var length = arguments.length;   //  实际传入参数的长度
            if (length == 0) {
                page = 1;
            }
            table.reload("listReloadLose", { //此处是上文提到的 初始化标识id
                where: {
                    "orderNo": $("#orderLose").val(),
                    "createTime": $("#createLose").val(),
                    "endDate": $("#endDateLose").val(),
                    "dealerId": $("#dealerIdLose").val(),
                    "invalidType": $("#invalidType").val()
                }, page: {
                    curr: page //重新从第 page 页开始 默认从第一页开始重载
                }
            });
        };
       
        //监听意向单工具条
        purchaseLose.on('tool(listLoseOrderTable)', function(obj){
              var data = obj.data;
              if(obj.event === 'detail'){
            	  dataDetail = data;
                  var index = layer.open({
                      type: 2
                      ,id: (new Date()).valueOf()
                      ,title: '进入'
                     ,content:  hostUrl +'/purchase/loseOrderDetail.do' 
                      ,area: ['500px', '500px']
                      ,maxmin: true
                      ,success: function(layero, index){
                          var body = layer.getChildFrame('body', index);
                          var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                      }
                      ,yes: function (index,layero) {

                          //return false 开启该代码可禁止点击该按钮关闭
                      }
                      ,btn1: function (index,layero) {
                          //return false 开启该代码可禁止点击该按钮关闭
                      }
                  });
                  layer.full(index);
              }
              
          });

        //图片预览
        function photoReview(url){
            layer.photos({
                photos: {
                    title: "查看",
                    data: [{
                        src: url
                    }]
                },
                shade: .01,
                closeBtn: 1,
                anim: 5
            })
        }
    });
});